<?php
/*
商品详情
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$basic=$this->setting;
$return['set']=$basic;
$return['setting']=$basic;
if($_RQ['op']=='main'){
	$id=$_RQ['id'];
	if(!$id){
		exi('未找到日志!','error');
	}
	$log=pdo_get('jy_creditshop_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$goods=pdo_get('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['goodsid']));
	if($log['optionid']){
		$option=pdo_get('jy_creditshop_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$log['goodsid'],'id'=>$log['optionid']));
		$goods['price']=$option['price'];
		$goods['credit']=$option['credit'];
		$goods['thumb']=$option['thumb']?:$goods['thumb'];
		$goods['total']=$option['total'];
	}
	$goods['storeids']=$goods['storeids']?json_decode($goods['storeids'],true):array();
	if(count($goods['storeids'])){
			$carrier_list=array();
			foreach($goods['storeids'] as $sv){
				$carrier_list[] = pdo_get('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'id'=>$sv['id']));
			}	
	}else{
			$carrier_list = pdo_getall('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
	}
	if($carrier_list){
				foreach($carrier_list as &$pv){
					$pv['pcd']=json_decode($pv['pcd'],true);
				}
	}
	$goods['stores']=$carrier_list;
	$address = false;
	if (!(empty($log['addressid']))) {			
		$address = pdo_get('core_address',array('uniacid' => $_SESSION['uniacid'],'id'=>$log['addressid'],'mid' =>$_SESSION['uid']));
	}
	//整理收货地址信息
	$member_address=pdo_getall('core_address',array('mid'=>$_SESSION['uid']));
	if($member_address){
		foreach($member_address as &$address){
			$address['pcd']=json_decode($address['pcd'],true);
			if($address['isdefault']){
				$item['addressid']=$address['id'];
				$item['address']=$address;
				break;
			}
		}
		if(!$item['addressid']){
			$item['addressid']=$member_address['0']['id'];
			$item['address']=$member_address['0'];
		}
	}
	$goods['money']=intval($goods['money']);
	$goods['dispatch']=intval($goods['dispatch']);
	$log['createtime'] = date('Y-m-d H:i:s', $log['createtime']);
	$log['time_send'] =$log['time_send']?date('Y-m-d H:i:s',$log['time_send']):'';
	$log['time_finish'] = $log['time_finish']?date('Y-m-d H:i:s',$log['time_finish']):'';	
	$log['verifycode'] = pdo_getcolumn('jy_creditshop_verify',array('uniacid'=>$_SESSION['uniacid'],'logid'=>$log['id']),'verifycode');
	$log['verifyurl']=custom_url('jy_creditshop/verify.detail//verifycode='.$log['verifycode'],true);
	$log['total']=$log['total']?:1;
	$log['express']=pdo_get('jy_weishop_express',array('id'=>$log['express']));
	$return['verify'] = pdo_get('jy_creditshop_verify',array('uniacid'=>$_SESSION['uniacid'],'logid'=>$log['id']));
	$log['statustitle'] = $this->order_status[$log['status']];
	if($goods['isverify']){
		if($log['status']==1){
			$log['statustitle'] = '待核销';
		}
		if($log['status']==3){
			$log['statustitle'] = '已兑换';
		}
	}else{
		if($goods['goodstype']==0 && $log['status']==1 && !$log['addressid']){
			$log['statustitle'] =$goods['type']==0?'待兑换，请选择收货地址':'已中奖，请选择收货地址';
		}
		if($goods['goodstype']==0 && $log['status']==1 && $log['addressid']){
			$log['statustitle'] = '待发货';
		}
	}
	if($goods['goodstype'] && $log['status']==1){
		$log['statustitle'] = '待兑换';
	}
	if($goods['goodstype'] && $log['status']==3){
		$log['statustitle'] = '已发放';
	}
	$log['credit_typetitle']=credit_types(true)[$log['credit_type']];
	if($log['taocan']){
		$log['taocan']=json_decode($log['taocan'],true);
		$log['taocan']['credit_title']=credit_types(true)[$log['taocan']['credit_type']];
		$log['totalprice']=$log['total']*$log['taocan']['money'];
	}else{
		$log['taocan']=array(
			'credit'=>$goods['credit'],
			'credit_title'=>credit_types(true)[$goods['credit_type']],
		);
		$log['totalprice']=$log['total']*$goods['money'];
	}
	$log['merchname']=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['paymerchid']),'merchname');
	$return['log']=$log;
	$return['stores']=$stores;
	$return['goods']=$goods;
	$return['item']=$item;
	exi($return);
}
//兑换
if($_RQ['op']=='exchange'){
	$id=$_RQ['id'];
	$log = pdo_get('jy_creditshop_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		if (empty($log)) {
			exi('未找到要兑换记录!','error');
		}
		if (empty($log['status'])) {			
			exi('还未完成兑换!','error');
		}
		if (3 <= $log['status']) {
			exi('此记录已兑换过了!','error');
		}

	$goods=pdo_get('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['goodsid']));
	if($log['optionid']){
		$option=pdo_get('jy_creditshop_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$log['goodsid'],'id'=>$log['optionid']));
		$goods['price']=$option['price'];
		$goods['credit']=$option['credit'];
	}
		if (($goods['isendtime'] == 1) && ($goods['endtime'] < time())) {
			exi('超出使用有效期，无法进行兑换!','error');
		}

		if (empty($goods['id'])) {
			
			exi('商品记录不存在!','error');
		}

		if (empty($goods['isverify'])) {
			
			exi('此商品不支持线下兑换!','error');
		}

		if (!empty($goods['type'])) {
			if ($log['status'] <= 1) {
			
				exi('未中奖，不能兑换!','error');
			}
		}

		if ((0 < $goods['money']) && empty($log['paystatus'])) {
			
			exi('未支付，无法进行兑换!','error');
		}

		if ((0 < $goods['dispatch']) && empty($log['dispatchstatus'])) {
			
			exi('未支付运费，无法进行兑换!','error');
		}

		if (($goods['isendtime'] == 1) && ($goods['endtime'] < $goods['currenttime'])) {
			
			exi('超出使用有效期，无法进行兑换!','error');
		}

		$time = time();
		pdo_update('jy_creditshop_log', array('status' => 3, 'usetime' => $time), array('id' => $log['id']));
		exi('成功兑换');
}